package com.bonana.utils;

import java.security.MessageDigest;

/**
 * sha1加密算法工具类
 * @author yangyl
 * 2012.05.11
 */
public class Sha1Util {
	
	public static String SHA1Encode(String password) {
		 String resultString = null;
		 try {
		    resultString = new String(password);
		    MessageDigest md = MessageDigest.getInstance("SHA-1");
		    resultString = byte2hexString(md.digest(resultString.getBytes()));
		 } catch (Exception ex) {
		 }
		 return resultString;
	}
	 
	public static final String byte2hexString(byte[] bytes) {
		  StringBuffer buf = new StringBuffer(bytes.length * 2);
		  for (int i = 0; i < bytes.length; i++) {
			   if (((int) bytes[i] & 0xff) < 0x10) {
				   buf.append("0");
			   }
			   buf.append(Long.toString((int) bytes[i] & 0xff, 16));
		  }
		  return buf.toString().toLowerCase();
	}
	 
	public static void main(String[] args) throws Exception {
             MessageDigest md = MessageDigest.getInstance("SHA-1");
		  System.out.println(SHA1Encode("hello"));
		  System.out.println(SHA1Encode("888888"));
		 
	}
}
